import Vue from 'vue'
import Vuex, { Module, ActionContext } from 'vuex'

const LOCAL_KEY_NICK_NAME = "user_nick_name";

let state = {
    id: 0,
    nick_name: ""
};

export default {
    namespaced: true,
    state,
    mutations: {
        set_id(state, payload: number) {
            state.id = payload;
        },
        set_nick_name(state, payload: string) {
            state.nick_name = payload;
            window.localStorage.setItem(LOCAL_KEY_NICK_NAME, payload);
        }
    },
    actions: {
        init({ state, commit }, payload?: any) {
            let nickname = window.localStorage.getItem(LOCAL_KEY_NICK_NAME);
            if (nickname) {
                commit("set_nick_name", nickname);
            }
        }
    }
} as Module<typeof state, any>;